import 'package:get/get.dart';
import 'package:realm/realm.dart';

import '../../models/contract_model.dart';

class ReminderController extends GetxController {
  final RxList<Contract> dataList = <Contract>[].obs;
  late Realm realm;
  final isLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    initRealm();
    loadExpiringContracts();
  }

  void initRealm() {
    realm = Realm(Configuration.local([Contract.schema]));
  }

  void loadExpiringContracts() {
    isLoading.value = true;
    try {
      // 获取7天内到期的合同
      final now = DateTime.now();
      final endDate = now.add(const Duration(days: 30));

      final results = realm.all<Contract>().query(
          'expirationDate >= \$0 AND expirationDate <= \$1',
          [now, endDate]).toList();

      dataList.assignAll(results);
      // TODO: 更新UI显示
    } finally {
      isLoading.value = false;
    }
  }

  @override
  void onClose() {
    realm.close();
    super.onClose();
  }
}
